package GUI;

import Run_System.Equation;

import javax.swing.*;
import javax.swing.event.DocumentEvent;
import javax.swing.event.DocumentListener;
import java.awt.*;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.ListIterator;

public class ShowEquationPanel extends JPanel {
    private  ArrayList<Integer> answers;
    private int currentEquation;
    public ShowEquationPanel(Equation[] exercises, ArrayList<Integer> answers,int index){
        this.setLayout(new GridLayout(5,1,10,10));
        this.answers = answers;
        this.currentEquation = index;

        for ( currentEquation = index; currentEquation < exercises.length && currentEquation < index+5; currentEquation++){       //将习题面板加入显示面板中，习题面板包含了左侧算式和右侧答案输入区
            JPanel panel = AddEquation(exercises[currentEquation],currentEquation);
            System.out.println(currentEquation);
            this.add(panel);
        }
    }


    protected int returnCurrentEquation(){
        return currentEquation;
    }

    private JPanel AddEquation(Equation equation,int index){

        JPanel panel = new JPanel();

        JTextField textField = new JTextField(equation.toString(1));  //左侧显示算式
        textField.setFont(new Font("微软雅黑",Font.BOLD,20));
        textField.setHorizontalAlignment(JTextField.CENTER);
        panel.add(textField);

        JTextField answer; //右侧输入答案,如答案文件里的答案不为-1，则表明小明做过这个习题，则把答案也显示出来
        int ans = answers.get(index);
        if(ans != -1){
            answer = new JTextField(""+ans);
        }else {
            answer = new JTextField();
        }
        answer.setFont(new Font("微软雅黑",Font.BOLD,20));
        answer.getDocument().addDocumentListener(new DocumentListener() {
            @Override
            public void insertUpdate(DocumentEvent e) {
                try {
                    answers.set(index,Integer.parseInt(answer.getText())); //获取小明输入的答案
                }catch (Exception exception){
                    JOptionPane.showMessageDialog(null,"答案要是数字才可以","坚持就是胜利",JOptionPane.WARNING_MESSAGE);
                }
            }

            @Override
            public void removeUpdate(DocumentEvent e) {                 //每次重新输入都会更新答案
                try {
                    System.out.println(answer.getText());
                    answers.set(index,Integer.parseInt(answer.getText()));
                }catch (Exception exception){
                   // JOptionPane.showMessageDialog(null,"答案要是数字才可以","坚持就是胜利",JOptionPane.WARNING_MESSAGE);
                }
            }

            @Override
            public void changedUpdate(DocumentEvent e) {
                try {
                    System.out.println(answer.getText());
                    answers.set(index,Integer.parseInt(answer.getText()));
                }catch (Exception excetpion){
                    JOptionPane.showMessageDialog(null,"答案要是数字才可以","坚持就是胜利",JOptionPane.WARNING_MESSAGE);
                }
            }
        });
        answer.setColumns(10);
        panel.add(answer);
        return panel;
    }
}
